package org.clockworkmages.games.anno1186.controllers;

import org.clockworkmages.games.anno1186.GameGuiService;
import org.clockworkmages.games.anno1186.GameState;
import org.clockworkmages.games.anno1186.GameStateService;
import org.clockworkmages.games.anno1186.Injected;
import org.clockworkmages.games.anno1186.SituationService;
import org.clockworkmages.games.anno1186.gui.LocalMapPanel;
import org.clockworkmages.games.anno1186.model.Direction;
import org.clockworkmages.games.anno1186.model.GameMode;
import org.clockworkmages.games.anno1186.model.map.tactical.Tile;
import org.clockworkmages.games.anno1186.model.map.tactical.TileObject;

public class LocalMapMovementController {

	@Injected
	private GameStateService gameStateService;

	@Injected
	private GameGuiService gameGuiService;
	@Injected
	private SituationService situationService;

	public void move(String keyPressed) {
		GameState gameState = gameStateService.getGameState();
		boolean stateAllowsMovement = GameMode.LOCAL_MAP.equals(gameState
				.getGameMode());
		if (stateAllowsMovement) {
			Direction direction;
			switch (keyPressed) {
			case "w":
				direction = Direction.NORTH;
				break;
			case "d":
				direction = Direction.EAST;
				break;
			case "s":
				direction = Direction.SOUTH;
				break;
			case "a":
				direction = Direction.WEST;
				break;
			default:
				// not recognized, so not moving anywhere:
				return;
			}
			move(direction, gameState);
		}
	}

	private void move(Direction direction, GameState gameState) {
		int currentX = gameState.getLocalMapX();
		int currentY = gameState.getLocalMapY();
		int targetX = currentX + direction.getX();
		int targetY = currentY + direction.getY();

		if (targetX < 0) {
			// TODO: check if the player can leave this area by going WEST. If
			// so, present a Situation screen asking him if he wants to continue
			// in that direction or return to the camp.
		} else if (targetY < 0) {
			// TODO: check if the player can leave this area by going SOUTH
		}

		Tile[][] tiles = gameState.getGameArea().getTiles();
		if (targetX == tiles.length) {
			// TODO: check if the player can leave this area by going EAST
		} else if (targetY == tiles[targetX].length) {
			// TODO: check if the player can leave this area by going NORTH
		}

		Tile targetTile = tiles[targetX][targetY];
		String situationId = null;
		boolean isPassable = true;
		for (TileObject tileObject : targetTile.getTileObjects()) {
			if (tileObject.getSituationId() != null) {
				situationId = tileObject.getSituationId();
				isPassable = false;
				break;
			} else if (!tileObject.isPassable()) {
				isPassable = false;
			}

		}

		if (situationId != null) {
			gameStateService.addSituation(situationId, true);
			situationService.playCurrentSituation();
		} else if (!isPassable) {
			// show the "The path is blocked" message.
		} else {
			gameState.setLocalMapX(targetX);
			gameState.setLocalMapY(targetY);
			// run the NPC turn, run perception tests etc.
			LocalMapPanel localMapPanel = gameGuiService.getLocalMapPanel();
			localMapPanel.repaint();
		}

	}
}
